Streaming Price with Oanda V20 API

Oanda API V20 Streaming

Oanda Streaming Documentation


In [1]:
import json
from oandapyV20 import API
from oandapyV20.exceptions import V20Error
from oandapyV20.endpoints.pricing import PricingStream

In [2]:
accountID = ""
access_token=""

In [3]:
api = API(access_token=access_token, environment="practice")

In [4]:
instruments = "EUR_USD"

In [5]:
s = PricingStream(accountID=accountID, params={"instruments":instruments})

In [6]:
try:
    n = 0
    for R in api.request(s):
        print(json.dumps(R, indent=2))
        n += 1
        if n > 5:
            print("done")
            break

except V20Error as e:
    print("Error: {}".format(e))


{
  "type": "PRICE",
  "closeoutBid": "1.18271",
  "status": "tradeable",
  "instrument": "EUR_USD",
  "tradeable": true,
  "time": "2017-08-14T03:14:25.300845871Z",
  "closeoutAsk": "1.18313",
  "asks": [
    {
      "liquidity": 10000000,
      "price": "1.18298"
    }
  ],
  "bids": [
    {
      "liquidity": 10000000,
      "price": "1.18286"
    }
  ]
}
{
  "type": "HEARTBEAT",
  "time": "2017-08-14T03:14:42.626381576Z"
}
{
  "type": "HEARTBEAT",
  "time": "2017-08-14T03:14:47.658447687Z"
}
{
  "type": "HEARTBEAT",
  "time": "2017-08-14T03:14:52.696389795Z"
}
{
  "type": "HEARTBEAT",
  "time": "2017-08-14T03:14:57.728078948Z"
}
{
  "type": "PRICE",
  "closeoutBid": "1.18271",
  "status": "tradeable",
  "instrument": "EUR_USD",
  "tradeable": true,
  "time": "2017-08-14T03:15:00.715775443Z",
  "closeoutAsk": "1.18314",
  "asks": [
    {
      "liquidity": 10000000,
      "price": "1.18299"
    }
  ],
  "bids": [
    {
      "liquidity": 10000000,
      "price": "1.18286"
    }
  ]
}
done